package net.caixiaomi.info.ui.discovery;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.google.gson.reflect.TypeToken;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.ActiveItemAdapter;
import net.caixiaomi.info.adapter.OpenPrizeFootBallAdapter;
import net.caixiaomi.info.base.BaseActivity;
import net.caixiaomi.info.base.progressbar.CircularProgressBar;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.base.quickadapter.entity.MultiItemEntity;
import net.caixiaomi.info.model.ActiveEntity;
import net.caixiaomi.info.model.ActiveListEntity;
import net.caixiaomi.info.model.BaseCallModel;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.CustomerModel;
import net.caixiaomi.info.model.OpenPrizeMatchListEntity;
import net.caixiaomi.info.net.RequestService;
import net.caixiaomi.info.net.ResponseCallback;
import net.caixiaomi.info.net.ResponseError;
import net.caixiaomi.info.net.RetrofitManage;
import net.caixiaomi.info.ui.dialog.TimeDialogFragment;
import net.caixiaomi.info.ui.view.MatchingSelectView;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.listener.OnRefreshListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

import static android.R.id.list;

/**
 * Created by mac on 2018/9/7.
 */

public class OpenPrizeFootballAcitivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener, OnRefreshListener {

    private TimePickerView pvTime;
    private Context mContext;
    private String mCurrentTime = "";
    private String lotteryClassify;
    private OpenPrizeFootBallAdapter mAdapter;

    @BindView(R.id.matching_date_iv)
    RelativeLayout mSelectView;
    @BindView(list)
    RecyclerView mListView;
    @BindView(R.id.refresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.progress)
    CircularProgressBar mProgress;
    @OnClick(R.id.matching_date_iv)
    void matchingDate(){
        pvTime.show(mSelectView);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
        mRefresh.setOnRefreshListener(this);
        mListView.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter=new OpenPrizeFootBallAdapter(null);
        mAdapter.setOnItemClickListener(this);
        mListView.setAdapter(mAdapter);
        initTimePicker();
        Date date=new Date();
        mCurrentTime=getTime(date);
        loadData();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        loadData();
    }

    @Override
    protected int layout() {
        return R.layout.open_prize_football_layout;
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }

    private void initTimePicker() {//Dialog 模式下，在底部弹出

        pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
//                Toast.makeText(OpenPrizeFootballAcitivity.this, getTime(date), Toast.LENGTH_SHORT).show();
                mCurrentTime=getTime(date);
                loadData();
                Log.i("pvTime", "onTimeSelect");

            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");
                    }
                })
                .setType(new boolean[]{true, true, true, false, false, false})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
            }
        }
    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        Log.d("getTime()", "choice date millis: " + date.getTime());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    private void loadData(){
        JSONObject params = new JSONObject();
        params.put("dateStr", mCurrentTime);
        params.put("lotteryClassify", "1");
        RequestService requestService = RetrofitManage.getInstance().getRequestService();
        Call<BaseCallModel<OpenPrizeMatchListEntity>> dis = requestService.getDiscoveryMatch(params);
        dis.enqueue(new ResponseCallback<BaseCallModel<OpenPrizeMatchListEntity>>() {
            @Override
            public void onSuccess(BaseCallModel<OpenPrizeMatchListEntity> data) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
                OpenPrizeMatchListEntity entity=data.data;
                List<MultiItemEntity>list=new ArrayList<>();
                list.add(entity);
                list.addAll(entity.getList());
                mAdapter.setNewData(list);
            }

            @Override
            public void onFail(ResponseError error) {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }

            @Override
            public void onError() {
                mProgress.setVisibility(View.GONE);
                mRefresh.finishRefresh();
            }
        });
    }
}
